package com.android.camera2.common.settings;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import com.android.camera2.R;
import com.android.camera2.common.utils.LogUtil;
import com.android.camera2.host.app.CameraActivity;

import java.util.prefs.Preferences;

public class CameraPreferenceSetting extends PreferenceFragment {

    private static final String TAG = CameraPreferenceSetting.class.getSimpleName();
    public static final String KEY_SHUTTER_DELAY = "pref_shutter_delay";

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_settings);
        PreferenceManager.setDefaultValues(getActivity(), R.xml.pref_settings, false);
        setShutterDelay();
        PreferenceManager.getDefaultSharedPreferences(getContext())
                .registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.preference_background, container, false);
        ImageView menuBack = v.findViewById(R.id.iv_menu_back);
        menuBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtil.i(TAG, "camera setting back ");
                ((CameraActivity) getActivity()).removeSettingFragment();
            }
        });
        return v;
    }

    private void setShutterDelay() {
        ListPreference shutterDelayList = (ListPreference) findPreference(KEY_SHUTTER_DELAY);
        String[] strings = new String[]{"0s","3s", "5s", "10s"};
        shutterDelayList.setEntries(strings);
        shutterDelayList.setEntryValues(strings);

    }

    /**
     * ListPreference中的数据主要通过SharedPreferences去拿
     * 每个item点击之后的值会自动存到SharedPreferences里面
     */
    private SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            Preference preferences = findPreference(key);
            switch (key){
                case KEY_SHUTTER_DELAY:
                    LogUtil.i(TAG,"__getEntryValues = " + ((ListPreference)preferences).getEntryValues().toString());
                    LogUtil.i(TAG,"__getValue = " + ((ListPreference)preferences).getValue());
                    preferences.setSummary(((ListPreference) preferences).getEntry());
                    break;
            }
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.i(TAG,"camera setting on destroy");
        PreferenceManager.getDefaultSharedPreferences(getContext())
                .unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
    }
}
